#!/bin/sh

ARGS="$*"

#
# get device pcmcia_slot
#     format: get_dev_pcmcia_slot [slot number]
#                 slot number: 0, 1
#     output: device name (hda or hdc)
#
get_dev_pcmcia()
{
while read SOCKET CLASS DRIVER INSTANCE DEVS MAJOR MINOR;
do
    echo $DEVS
done
}
get_dev_pcmcia_slot()
{
    grep "^$1" /var/lib/pcmcia/stab | get_dev_pcmcia
}

if test -z $ARGS ; then


    BOOT_NO="1"

    echo "Root Filesystem selecter."
    echo
    echo "1. Qtopia"
    echo "2. pdaXrom"
    echo "3. Compact Flash"
#    echo "4. SD card"
    echo
#    echo  "After 20 seconds, boot Qtopia's root filesystem"
    echo
    echo -n "Select :"
    read BOOT_NO
    
    if [ $BOOT_NO = "2" ] ; then  # 2. /dev/hda4(hdd4) root filesystem
    
	/bin/mount -t proc none /proc
	/bin/mount -t jffs2 -o ro /dev/mtdblock3 /home
   
        /etc/rc.d/init.d/functions

        # mount /dev
        dd if=/dev/zero of=/dev/ram1 bs=1024 count=64 2> /dev/null > /dev/null
        /sbin/mkfs.minix -i 512 /dev/ram1 64 2> /dev/null > /dev/null
        mount -t minix /dev/ram1 /dev
        (cd /home ; tar cf - dev | (cd / ; tar xf -))
 
        # mount /tmp
        mount -t tmpfs -o size=1m none /dev/shm
        mkdir /dev/shm/tmp
        chmod 4777 /dev/shm/tmp
        mkdir /dev/shm/run
        touch /dev/shm/run/utmp
        mkdir /dev/shm/run/usb
    
        mount -t tmpfs -o size=1m none /var
        mkdir /var/run
	mkdir /var/lib
	mkdir /var/lib/pcmcia

        /etc/ledmode reset

        # mount HDD
        /etc/rc.d/init.d/pcmcia start

	while [ "$IDE1" = "" ] ; do
	    IDE1=`get_dev_pcmcia_slot 1`
	done

        /bin/mount -t ext3 /dev/${IDE1}4 /hdd4 # mount hdd4 on /new-root

	if [ -e /hdd4/sbin/init ] ; then
	
	    /bin/umount /var 
	    /etc/rc.d/init.d/pcmcia stop >/dev/null 2>&1
	    /bin/umount /home
	    /bin/umount /dev/shm
	    /bin/umount /dev
	    /bin/umount /proc
	    
	    cd /hdd4
	    sbin/pivot_root . old-root  # old-root is in the NEW filesystem,
#	    sbin/pivot_root . /  # old-root is in the NEW filesystem,
                                        # pivot_root changes the rootfilesystem
#	    chroot /hdd4
	    exec /sbin/init <dev/console >dev/console 2>&1
#	    exec /sbin/chroot .  /sbin/init <dev/console >dev/console 2>&1
	else
	    /bin/umount /hdd4
	    /etc/rc.d/init.d/pcmcia stop >/dev/null 2>&1
	    /bin/umount /var
	    /bin/umount /home
	    /bin/umount /dev/shm
	    /bin/umount /dev
	    /bin/umount /proc

	    exec /sbin/telinit
	fi
    elif [ $BOOT_NO = "3" ] ; then  # 3. Compact Flash root filesystem
    
	/bin/mount -t proc none /proc
	/bin/mount -t jffs2 -o ro /dev/mtdblock3 /home
    
        /etc/rc.d/init.d/functions

        # mount /dev
        dd if=/dev/zero of=/dev/ram1 bs=1024 count=64 2> /dev/null > /dev/null
        /sbin/mkfs.minix -i 512 /dev/ram1 64 2> /dev/null > /dev/null
        mount -t minix /dev/ram1 /dev
        (cd /home ; tar cf - dev | (cd / ; tar xf -))

        # mount /tmp
        mount -t tmpfs -o size=1m none /dev/shm
        mkdir /dev/shm/tmp
        chmod 4777 /dev/shm/tmp
        mkdir /dev/shm/run
        touch /dev/shm/run/utmp
        mkdir /dev/shm/run/usb
    
        mount -t tmpfs -o size=1m none /var
        mkdir /var/run
	mkdir /var/lib
	mkdir /var/lib/pcmcia

        /etc/ledmode reset

        # mount HDD
        /etc/rc.d/init.d/pcmcia start
	while [ "$IDE0" = "" ] ; do
	    IDE0=`get_dev_pcmcia_slot 0`
	done

        /bin/mount -t ext3 /dev/${IDE0}1 /hdd3 # mount CF partition 1 on /hdd3

	if [ -e /hdd3/sbin/init ] ; then
	
	    cd /hdd3
   	
	    /etc/rc.d/init.d/pcmcia stop >/dev/null 2>&1
	    /bin/umount /var
	    /bin/umount /home
	    /bin/umount /dev/shm
	    /bin/umount /dev
	    /bin/umount /proc
	    
	    sbin/pivot_root . old-root  #old-root is in the NEW filesystem,
                                        #pivot_root changes the rootfilesystem
	    
	    exec /sbin/chroot .  /sbin/init <dev/console >dev/console 2>&1
	else
	    /bin/umount /hdd3
	    /etc/rc.d/init.d/pcmcia stop >/dev/null 2>&1
	    /bin/umount /var
	    /bin/umount /home
	    /bin/umount /dev/shm
	    /bin/umount /dev
	    /bin/umount /proc

	    exec /sbin/telinit
	fi
    elif [ $BOOT_NO = "4" ] ; then  # 4. SD root filesystem
	/bin/mount -t proc none /proc
/bin/sh
	/sbin/insmod /lib/modules.rom/2.4.20/kernel/drivers/block/sharp_mmcsd_m.o

	echo "Please re-insert SD card or insert SD card."
	read TEMP
	/sbin/sdctrl insert 0

	/bin/mount -t ext2 /dev/mmcda1 /hdd3 # mount SD card partition 1 on /hdd3

	if [ -e /hdd3/sbin/init ] ; then
	    
	    cd /hdd3
	    /bin/umount /proc

	    sbin/pivot_root . old-root  #old-root is in the NEW filesystem,
                                        #pivot_root changes the rootfilesystem
	    
#	    exec /sbin/chroot .  /sbin/init <dev/console >dev/console 2>&1
	    /sbin/chroot .  
            /sbin/init <dev/console >dev/console 2>&1
	else
	    cd /
	    umount /new-root
	    rmmod sharp_mmcsd_m
	    /bin/umount /proc
	    exec /sbin/telinit
	fi

    else  # 1. Qtopia root filesystem
	exec /sbin/telinit
    fi
    
else
    exec /sbin/telinit $ARGS
fi
